package xyz.yuzegod.megawalls.listener;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.ClassesManager;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.inventory.InventoryManager;

public class InventoryListener extends BaseListener
{
    private static Game game;
    
    public InventoryListener(final MegaWalls plugin) {
        super(plugin);
    }
    
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onInventoryClick(final InventoryClickEvent e) {
        if (!(e.getWhoClicked() instanceof Player)) {
            return;
        }
        final ItemStack item = e.getCurrentItem();
        final Player player = (Player)e.getWhoClicked();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        if (item == null) {
            return;
        }
        if (item.getType() == Material.AIR) {
            return;
        }
        if (gamePlayer.isSpectator()) {
            e.setCancelled(true);
            switch (item.getType()) {
                case COMPASS: {
                    if (item.hasItemMeta() && item.getItemMeta().getDisplayName().contains("传送器")) {
                        InventoryListener.game.openSpectatorInventory(gamePlayer);
                        break;
                    }
                    break;
                }
                case DIODE: {
                    InventoryManager.SPECTATORSETTINGS.open(player);
                    break;
                }
                case BED: {
                    MegaWalls.getInstance().tpToLobby(player);
                    break;
                }
            }
            if (e.getInventory().getName().equals("§a§l传送器")) {
                e.setCancelled(true);
                final ItemStack eitem = e.getCurrentItem();
                if (eitem == null || !eitem.getType().equals((Object)Material.SKULL_ITEM)) {
                    return;
                }
                final SkullMeta meta = (SkullMeta)eitem.getItemMeta();
                final Player p = Bukkit.getServer().getPlayer(meta.getOwner());
                if (p == null) {
                    return;
                }
                player.closeInventory();
                player.teleport((Entity)p);
                return;
            }
        }
        if (InventoryListener.game.isWaiting()) {
            e.setCancelled(true);
            if (item.getType() == Material.BED) {
                MegaWalls.getInstance().tpToLobby(player);
            }
        }
        else if (InventoryListener.game.isStarted() && !gamePlayer.isSpectator() && ClassesManager.isClassesItem(item)) {
            if (player.getOpenInventory().getType() != InventoryType.CRAFTING && e.getClickedInventory().getType() == InventoryType.PLAYER) {
                e.setCancelled(true);
                player.sendMessage("§c无法将职业套装物品移出物品栏！");
                return;
            }
            if (player.getOpenInventory().getType() != InventoryType.CRAFTING && e.getClickedInventory().getType() != InventoryType.PLAYER) {
                e.setCancelled(true);
                player.sendMessage("§c无法将职业套装物品移出物品栏！");
                return;
            }
            if (e.getClickedInventory().getType() == InventoryType.PLAYER && (e.getRawSlot() == 5 || e.getRawSlot() == 6 || e.getRawSlot() == 7 || e.getRawSlot() == 8)) {
                e.setCancelled(true);
            }
        }
    }
    
    static {
        InventoryListener.game = MegaWalls.getInstance().getGame();
    }
}
